$(function () {
    findById();
});

/**
 * 查询id
 */
function findById() {
    let id = sessionStorage.getItem("userId");
    let result = myAjax("/admin/user/findById", {id: id},"post");
    console.log(result);
    if (result != null) {
        setData(result.model);
    }
}
//赋值
function setData(data) {
    console.log(data);
    //将数据库中的数据显示在页面时，属性为readonly只读不能修改
    $("#username").val(data.userName);
}
/**
 * 密码规则
 * @returns {boolean}
 */
function passwordRule() {
    let password = $("#password").val();
    //要求密码为至少包含一位数字和一位字母长度为6到20为之间
    let rule = /^(?![^a-zA-Z]+$)(?!\D+$)[0-9a-zA-Z]{6,20}$/;
    return judge(rule, password, "passwordTips", "您输入的格式错误", "");

}
function rePasswordRule() {
    let password = $("#rePassword").val();
    //要求密码为至少包含一位数字和一位字母长度为6到20为之间
    let rule = /^(?![^a-zA-Z]+$)(?!\D+$)[0-9a-zA-Z]{6,20}$/;
    return judge(rule, password, "rePasswordTips", "您输入的格式错误", "");

}

function judge(rule, value, tips, errorTips, successTips) {
    if (!rule.test(value)) {
        $("#" + tips).html(errorTips);
        return false;
    } else {
        $("#" + tips).html(successTips);
        return true;
    }

}

//提交表单时再一次判断提交数据是否正确
function save() {
    if (passwordRule() && rePasswordRule()) {
        let data = {
            id:sessionStorage.getItem("userId"),
            password:$("#password").val(),
            rePassword:$("#rePassword").val(),
        };
        //连接后台servlet
        let result = myAjax("/admin/user/editPassword", data,"post");
        if (result.result == "success") {
            alert("修改成功(=^ ^=)！")
            window.location.reload();
        }else if(result.result == "isHave") {
            alert("两次密码不相同，请重新输入！！！")
        } else {
            alert("修改失败，请重新输入！！！");
        }
    }
}
